package controllers

import (
	"github.com/neo4j/neo4j-go-driver/v4/neo4j"
)

func (c *AssessController) AssessTitle() {
	con0 := c.GetString("con0")
	con1 := c.GetString("con1")
	con2 := c.GetString("con2")
	con3 := c.GetString("con3")

	driver, err := neo4j.NewDriver(dbUri, neo4j.BasicAuth("neo4j", "980115", ""))
	if err != nil {
		panic(err)
	}
	defer driver.Close()
	session := driver.NewSession(neo4j.SessionConfig{})
	defer session.Close()
	results, _ := session.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) {
		var result neo4j.Result
		var titles []AssessT

		if con0 == "" {
			result, _ = tx.Run(`match (a:Assess) return distinct a.class as class`, map[string]interface{}{})
			for result.Next() {
				record := result.Record()
				title_int, _ := record.Get("class")
				var assess AssessT
				assess.Title, _ = title_int.(string)
				titles = append(titles, assess)
			}
		} else if con1 == "" {
			result, _ = tx.Run(`match (a:Assess{class:$class}) return distinct a.item as item`, map[string]interface{}{"class": con0})
			for result.Next() {
				record := result.Record()
				title_int, _ := record.Get("item")
				var assess AssessT
				assess.Title, _ = title_int.(string)
				titles = append(titles, assess)
			}
		} else if con2 == "" {
			result, _ = tx.Run(`match (a:Assess{class:$class,item:$item}) return distinct a.subitem as subitem`,
				map[string]interface{}{"class": con0, "item": con1})
			for result.Next() {
				record := result.Record()
				title_int, _ := record.Get("subitem")
				var assess AssessT
				assess.Title, _ = title_int.(string)
				titles = append(titles, assess)
			}
		} else if con3 == "" {
			result, _ = tx.Run(`match (a:Assess{class:$class,item:$item,subitem:$subitem}) return distinct a.target as target`,
				map[string]interface{}{"class": con0, "item": con1, "subitem": con2})
			for result.Next() {
				record := result.Record()
				title_int, _ := record.Get("target")
				var assess AssessT
				assess.Title, _ = title_int.(string)
				titles = append(titles, assess)
			}
		} else {
			result, _ = tx.Run(`match (a:Assess{class:$class,item:$item,subitem:$subitem,target:$target}) return distinct a.example as example`,
				map[string]interface{}{"class": con0, "item": con1, "subitem": con2, "target": con3})
			for result.Next() {
				record := result.Record()
				title_int, _ := record.Get("example")
				var assess AssessT
				assess.Title, _ = title_int.(string)
				titles = append(titles, assess)
			}
		}
		return titles, nil
	})

	c.Data["json"] = results
	c.ServeJSON()
}
